﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class PasswordRecovery : System.Web.UI.Page
{
    UsersBLL usersLogic = new UsersBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated)
        {
            ModalPopupMessage.ShowModalPopup_Redirect("ILE Message", "You're already logged in! Please log out and try.", 2, "MyClasses.aspx");
        }
    }

    protected void WizardPasswordRecovery_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        Response.Redirect("Login.aspx");
    }
    protected void WizardPasswordRecovery_NextButtonClick(object sender, WizardNavigationEventArgs e)
    {
        if (e.NextStepIndex == 1)
        {
            Validate("PasswordRecovery");
            if (!Page.IsValid)
            {
                e.Cancel = true;
                return;
            }

            string email = txtEmail.Text;

            switch (usersLogic.IsUserInTable(email))
            {
                case 1:
                    break;
                case 0:
                    ModalPopupMessage.ShowModalPopup("ILE Message", "Your email address does not exist in our system.", 2);
                    e.Cancel = true;
                    return;
                case -1:
                    ModalPopupMessage.ShowModalPopup("ILE Message", "An error has occurred. Please try again.", 2);
                    e.Cancel = true;
                    return;
            }
            Users.UsersRow user = usersLogic.GetUserByUsername(email);
            Users.Users_NonInstituteRow nonInstitutUser = usersLogic.GetNonInstUserByUserID(user.UserID);
            lblSecurityQuestion.Text = nonInstitutUser.SecurityQuestion.ToString();
        }
        else if (e.NextStepIndex == 2)
        {
            string email = txtEmail.Text;
            string newPassword = txtNewPassword.Text;

            if(!usersLogic.ResetNonInstitutUserPassword(email, txtSecurityAnswer.Text, newPassword))
            {
                e.Cancel = true;
                ModalPopupMessage.ShowModalPopup("ILE Message", "An error has occurred. Please try again.", 2);
            }
        }
    }
}
